<html>
<head><meta charset="utf-8"><title>rust-analyser api-walkthrough question · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/rust-analyser.20api-walkthrough.20question.html">rust-analyser api-walkthrough question</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="218949598"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/rust-analyser%20api-walkthrough%20question/near/218949598" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/rust-analyser.20api-walkthrough.20question.html#218949598">(Dec 05 2020 at 19:34)</a>:</h4>
<p>Hi, </p>
<p>I went through the Rust-analyser walkthrough method and println!'d the following variable: <a href="https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/syntax/src/lib.rs#L391">https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/syntax/src/lib.rs#L391</a></p>
<p>On my computer I got the following expressions:</p>
<div class="codehilite"><pre><span></span><code>exprs_visit = [
    &quot;{\n            1 + 1\n        }&quot;,
    &quot;1 + 1&quot;,
    &quot;1&quot;,
    &quot;1&quot;,
]
</code></pre></div>
<p>for</p>
<div class="codehilite"><pre><span></span><code>fn foo() {
1 + 1
}
</code></pre></div>
<p>The first two expressions are essentially  the same, is this expected?</p>



<a name="218949706"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/rust-analyser%20api-walkthrough%20question/near/218949706" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/rust-analyser.20api-walkthrough.20question.html#218949706">(Dec 05 2020 at 19:37)</a>:</h4>
<p>That's the block expression <code>{ 1 + 1 }</code>, which is different from <code>1  + 1</code> (which is the blocks <em>trailing</em> expression)</p>



<a name="218949710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/rust-analyser%20api-walkthrough%20question/near/218949710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/rust-analyser.20api-walkthrough.20question.html#218949710">(Dec 05 2020 at 19:37)</a>:</h4>
<p>For the general case, I think it could be expected since you may want to state that a block is an expression?</p>



<a name="218949768"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/rust-analyser%20api-walkthrough%20question/near/218949768" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/rust-analyser.20api-walkthrough.20question.html#218949768">(Dec 05 2020 at 19:38)</a>:</h4>
<p>Function bodies are represented as block expressions in r-a's syntax tree</p>



<a name="218949846"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/rust-analyser%20api-walkthrough%20question/near/218949846" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/rust-analyser.20api-walkthrough.20question.html#218949846">(Dec 05 2020 at 19:40)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/rust-analyser.20api-walkthrough.20question/near/218949768">said</a>:</p>
<blockquote>
<p>Function bodies are represented as block expressions in r-a's syntax tree</p>
</blockquote>
<p>Oh that makes sense, thanks for clearing that up. Am I also correct in saying that if I wanted to get the function foo twice, it would require a full traversal of the tree each time?</p>
<p>ie I cannot directly get foo based on it's name "foo" since no map exists?</p>



<a name="218949902"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/rust-analyser%20api-walkthrough%20question/near/218949902" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/rust-analyser.20api-walkthrough.20question.html#218949902">(Dec 05 2020 at 19:42)</a>:</h4>
<p>I think so? I don't see another way to search the syntax tree otherwise</p>



<a name="218949966"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/rust-analyser%20api-walkthrough%20question/near/218949966" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/rust-analyser.20api-walkthrough.20question.html#218949966">(Dec 05 2020 at 19:43)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/rust-analyser.20api-walkthrough.20question/near/218949902">said</a>:</p>
<blockquote>
<p>I think so? I don't see another way to search the syntax tree otherwise</p>
</blockquote>
<p>Ahh okay makes sense, thank you</p>



<a name="218950001"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/rust-analyser%20api-walkthrough%20question/near/218950001" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/rust-analyser.20api-walkthrough.20question.html#218950001">(Dec 05 2020 at 19:44)</a>:</h4>
<p>I mean, you can also just clone the <code>SyntaxNode</code> and reuse it</p>



<a name="218950080"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/rust-analyser%20api-walkthrough%20question/near/218950080" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/rust-analyser.20api-walkthrough.20question.html#218950080">(Dec 05 2020 at 19:45)</a>:</h4>
<p>not sure what you're after</p>



<a name="218950140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/rust-analyser%20api-walkthrough%20question/near/218950140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/rust-analyser.20api-walkthrough.20question.html#218950140">(Dec 05 2020 at 19:46)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/rust-analyser.20api-walkthrough.20question/near/218950080">said</a>:</p>
<blockquote>
<p>not sure what you're after</p>
</blockquote>
<p>Oh sorry it was a hypothetical optimisation. In most cases, I would do as you said above and clone the Rc</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>